package com.nuoniu.sibanyun.controller.crm;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nuoniu.sibanyun.common.BaseController;
import com.nuoniu.sibanyun.common.result.HttpResult;
import com.nuoniu.sibanyun.entity.crm.CrmFiles;
import com.nuoniu.sibanyun.service.crm.ICrmFilesService;
import com.nuoniu.sibanyun.userThread.UserInfo;
import com.nuoniu.sibanyun.userThread.UserThreadLocal;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 客户文档 前端控制器
 * </p>
 *
 * @author baiqiang
 * @since 2020-06-20
 */
@RestController
@RequestMapping("/crm/files")
@Api(value="客户文档 ",tags={"客户文档"})
public class CrmFilesController extends BaseController {

    @Autowired
    private ICrmFilesService filesService;

    /**
     * 分页获取客户文档列表
     * @author 小啊刚
     * @date 2020/7/9
     */
    @GetMapping("page")
    @ApiOperation(value = "分页",tags = "APP_客户文档")
    public HttpResult<Page<CrmFiles>> pageFilesList(Page page, CrmFiles files) {
        return HttpResult.ok(filesService.pageFilesList(page, files));
    }

    /**
     * 保存客户文档
     * @author 小啊刚
     * @date 2020/7/9
     */
    @PostMapping("save")
    @ApiOperation(value = "保存（新增/编辑）",tags = "APP_客户文档")
    public HttpResult saveFiles(@RequestBody CrmFiles files) {
        UserInfo userInfo = UserThreadLocal.get();
        if(files.getId() ==  null) {
            files.setCreateUserName(userInfo.getRealName());
            files.setCreateUserId(userInfo.getUserId());
        }else {
            files.setUpdateUserName(userInfo.getRealName());
            files.setUpdateUserId(userInfo.getUserId());
        }
        filesService.saveOrUpdate(files);
        return HttpResult.ok();
    }

    /**
     * 停用
     * @author 小啊刚
     * @date 2020/7/8
     */
    @PostMapping("disable")
    @ApiOperation(value = "停用",tags = "APP_客户文档")
    public HttpResult update(@RequestBody List<Long> ids) {
        for(Long id : ids) {
            CrmFiles files = new CrmFiles();
            files.setId(id);
            files.setStatus(2);
            filesService.updateById(files);
        }
        return HttpResult.ok();
    }

}
